home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / Utilities.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  9.8 KB  |  398 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Utilities.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14.  
  15. #ifndef __BLJSTANDARDINCLUDES__
  16. #include "BLJStandardIncludes.h"
  17. #endif
  18.  
  19. #ifndef __DEBUGGINGGEAR__
  20. #include "DebuggingGear.h"
  21. #endif
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26.  
  27. #ifndef __UTILITIES__
  28. #include "Utilities.h"
  29. #endif
  30.  
  31. #pragma segment Utilities
  32.  
  33. /***********************************|****************************************/
  34.  
  35. ostream& DumpHex (ostream& s, const void *pv, unsigned long size)
  36. {
  37.     register char* p = (char*) pv;
  38.  
  39.     if (size > 1024)
  40.         size = 1024;
  41.  
  42.     for (short i = 0; i < size; i += 16)
  43.     {
  44.         s << hexo << (long) (p + i) << ":" << deco;
  45.         for (short j = 0; j < 16; ++j)
  46.         {
  47.             char c = * (char *) (p + i + j);
  48.             if (i + j < size)
  49.                 s << hexo << ((c & 0xf0) >> 4) << (c & 0x0f) << " ";
  50.             else
  51.                 s << "   ";
  52.         }
  53.         s << " | ";
  54.         for (j = 0; j < 16; ++j)
  55.         {
  56.             char c = ( * (char *) (p + i + j)) & 0x7f;
  57.             if (c < 32) c = '•';
  58.             if (i + j < size)
  59.                 s << c;
  60.         }
  61.         s << deco << endl;
  62.     }
  63.  
  64.     return s;
  65. }
  66.  
  67. //--------------------------------------------------------------------------------
  68.  
  69. void GetRezMessage(short rezId, short problemIndex, RString& message) 
  70. {
  71.     Str255 theString;
  72.     GetIndString(theString, rezId, problemIndex);
  73.     message.charSet = smRoman;
  74.     message.dataLength = (unsigned short) longmin(theString[0], kRStringMaxChars);
  75.     BlockMove( &theString[1], (Ptr) &message.body, message.dataLength );
  76. }
  77.  
  78. //--------------------------------------------------------------------------------
  79.  
  80. unsigned long NowDateTime()
  81. {
  82.     unsigned long now;
  83.     GetDateTime(&now);
  84.     return now;
  85. }
  86.  
  87. /*-----------------------------------------------------------
  88. -    MatchResString:        Determines if a string exists in a
  89. -                        STR# list and returns the index
  90. -
  91. -        theStr:            string to search for
  92. -        startIndex:        inclusive start of the search
  93. -        endIndex:        inclusive end of search
  94. -        resID:            STR# res id to look in
  95. -
  96. -        returns:        the index of the string
  97. ------------------------------------------------------------
  98. */
  99. short MatchResString(const StringPtr theStr, short startIndex, short endIndex, short resID)
  100. {
  101.     Str255 resStr;
  102.     
  103.     for (short i = startIndex; i <= endIndex; i++)
  104.     {
  105.         GetIndString(resStr, resID, i);
  106.         
  107.         if (EqualString(resStr, theStr, false, false))
  108.             return(i);
  109.     }
  110.     
  111.     return(-1);
  112. }
  113.  
  114. //--------------------------------------------------------------------------------
  115. //
  116. //    GetDirIDForFolder()
  117. //    ===================
  118. //
  119. //    Return the dirID for a folder, given the vRefNum of the disk and the folder name.
  120. //    If the folder does not exist, return 0 for the dirID.
  121. //
  122. long GetDirIDForFolder(short vRefNum, long dirID, const StringPtr folderName) 
  123. {
  124.     CInfoPBRec paramBlock;
  125.     long result = 0;
  126.  
  127.     paramBlock.dirInfo.ioVRefNum = vRefNum;
  128.     paramBlock.dirInfo.ioDrDirID = dirID;
  129.     paramBlock.dirInfo.ioFDirIndex = 0;
  130.     paramBlock.dirInfo.ioNamePtr = (StringPtr) folderName;
  131.  
  132.     OSErr err = PBGetCatInfo(¶mBlock, false);
  133.  
  134.     if (err == noErr) 
  135.         if (BitTst(¶mBlock.dirInfo.ioFlAttrib, 3))  
  136.             result = paramBlock.dirInfo.ioDrDirID;
  137.  
  138.     return(result);
  139. }
  140.  
  141.  
  142. //--------------------------------------------------------------------------------
  143. //
  144. //    GetIndexHFSInfo
  145. //    ===============
  146. //
  147. //    This function returns information about the index-th file in the directory
  148. //    given by (vRefNum, dirRefNum).  It returns the name of the file/directory in
  149. //    theName, which must be a pointer to a Str32.  If the entry is a file, then
  150. //    refNum is a reference number for this file if the file is currently open, or
  151. //    0 if the file is not open.  If this entry is a directory, then refNum is the
  152. //    dirID for the directory.  isFile is either kFile or kFolder or kNotFileOrFolder,
  153. //    depending on what type of entry the given file is.
  154. //
  155. OSErr GetIndexHFSInfo(short index, short vRefNum, long dirRefNum, const StringPtr theName) 
  156. {
  157.     long refNum;
  158.     short isFile;
  159.     return GetIndexHFSInfo(index, vRefNum, dirRefNum, refNum, theName, isFile);
  160. }
  161.  
  162. /***********************************|****************************************/
  163.  
  164. OSErr GetIndexHFSInfo(short index, short vRefNum, long dirRefNum, long& refNum, const StringPtr theName, short& isFile) 
  165. {
  166.     CInfoPBRec paramBlock;
  167.     OSErr err;
  168.  
  169.     //    If we're getting info on an indexed file, then zero out the name.
  170.     if ((index > 0) && (theName)) 
  171.     {
  172.         theName[0] = 0;
  173.     }
  174.  
  175.     memset ( ¶mBlock, 0, sizeof(paramBlock));
  176.     paramBlock.dirInfo.ioVRefNum = vRefNum;
  177.     paramBlock.dirInfo.ioDrDirID = dirRefNum;
  178.     paramBlock.dirInfo.ioFDirIndex = index;
  179.     paramBlock.dirInfo.ioNamePtr = (StringPtr) theName;
  180.  
  181.     err = PBGetCatInfo(¶mBlock, false);
  182.  
  183.     if (err == noErr) 
  184.     {
  185.         if (BitAnd(paramBlock.dirInfo.ioDrUsrWds.frFlags, fInvisible) == 0) 
  186.         {
  187.             SignedByte flags = paramBlock.dirInfo.ioFlAttrib;
  188.             if (BitTst(&flags, 3))  
  189.             {                            // This is a folder
  190.                     refNum = paramBlock.dirInfo.ioDrDirID;
  191.                     isFile = kFolder;
  192.             } 
  193.             else 
  194.             {                                            // This a file
  195.                     refNum = paramBlock.dirInfo.ioFRefNum;
  196.                     isFile = kFile;
  197.             }
  198.         }
  199.         else
  200.             isFile = kNotFileOrFolder;
  201.  
  202.         return(noErr);
  203.     }
  204.  
  205.     return(err);
  206. }
  207.  
  208. //--------------------------------------------------------------------------------
  209. //
  210. //    CreateFolderIfItDoesntExist()
  211. //    =============================
  212. //
  213. //    This function creates a folder in the given vRefNum, dirID with a given name
  214. //    if it does not exist.  The dirID of the folder (existing or newly-created) is
  215. //    returned as the result of this function.
  216. //
  217. long CreateFolderIfItDoesntExist(short vRefNum, long parentDir, const StringPtr folderName) 
  218. {
  219.     long refNum;
  220.     short isFile;
  221.     Str255 temp;
  222.  
  223.     PLstrcpy( temp,folderName);
  224.     OSErr err = GetIndexHFSInfo(0, vRefNum, parentDir, refNum, temp, isFile);
  225.  
  226.     if (err != noErr) 
  227.     {
  228.         FSSpec spec;
  229.         err = FSMakeFSSpec(vRefNum,parentDir, temp,&spec);
  230.         err = FSpDirCreate(&spec,smRoman,&refNum);
  231.     }
  232.  
  233.     return refNum;
  234. }
  235.  
  236. //--------------------------------------------------------------------------------
  237. //
  238. //    DeleteFilesInFolder()
  239. //    =====================
  240. //
  241. //    This function deletes all of the files in the given folder.  It does not delete
  242. //    any folders in the directory, nor does it recursively delete files in any
  243. //    subfolders.
  244. //
  245. void DeleteFilesInFolder(short vRefNum, long folderID) 
  246. {
  247.     long refNum;
  248.     short isFile;
  249.     OSErr err = noErr;
  250.     Str255 temp;
  251.     
  252.     do
  253.     {
  254.         FSSpec    spec;
  255.  
  256.         err = GetIndexHFSInfo(1,vRefNum, folderID, refNum, temp, isFile);
  257.  
  258.         if ((err == noErr) && (isFile == kFile))
  259.         {
  260.             err = FSMakeFSSpec(vRefNum,folderID, temp,&spec);
  261.             FSpDelete(&spec);
  262.         }
  263.     } 
  264.     while (err == noErr);
  265. }
  266.  
  267.  
  268. /***********************************|****************************************/
  269.  
  270. void DeleteFilesInFolderOlderThan ( short vRefNum, long dirID, unsigned long deleteFilesBeforeDateTime)
  271. {
  272.     OSErr err = noErr;
  273.     unsigned short index = 1;
  274.     
  275.     do {    
  276.         CInfoPBRec paramBlock;
  277.         Str255 fileName;
  278.         
  279.         fileName[0] = 0;
  280.  
  281.         memset ( ¶mBlock, 0, sizeof(paramBlock));
  282.         paramBlock.dirInfo.ioVRefNum = vRefNum;
  283.         paramBlock.dirInfo.ioDrDirID = dirID;
  284.         paramBlock.dirInfo.ioFDirIndex = index;
  285.         paramBlock.dirInfo.ioNamePtr = (StringPtr) &fileName;
  286.  
  287.         err = PBGetCatInfoSync ( ¶mBlock );
  288.     
  289.         if (err == noErr) 
  290.         {
  291.             if (BitAnd(paramBlock.dirInfo.ioDrUsrWds.frFlags, fInvisible) == 0 )
  292.             {
  293.                 SignedByte flags = paramBlock.dirInfo.ioFlAttrib;
  294.                 if ( !(flags & 0x0010) )
  295.                 {    // This a file
  296.                     if ( paramBlock.hFileInfo.ioFlMdDat < deleteFilesBeforeDateTime )
  297.                         if (HDelete ( vRefNum, dirID, fileName ) == noErr )
  298.                         {
  299.                             ForceFinderToUpdateFolder ( vRefNum, dirID );
  300.                             index -- ;
  301.                         }
  302.                 }
  303.             }
  304.             index ++;
  305.         }    
  306.     } while ( err == noErr );
  307. }
  308.  
  309. //--------------------------------------------------------------------------------
  310. //
  311. //    ForceFinderToUpdateFolder()
  312. //    ===========================
  313. //
  314. //    Given a vRefNum and dirID, force the Finder to flush any cached info
  315. //    for the folder by touching the modification date for this folder.
  316. //
  317. OSErr ForceFinderToUpdateFolder(short vRefNum, long dirID) 
  318. {
  319.     OSErr err;
  320.     CInfoPBRec pb;
  321.  
  322.     pb.dirInfo.ioVRefNum = vRefNum;
  323.     pb.dirInfo.ioDrDirID = dirID;
  324.     pb.dirInfo.ioNamePtr = nil;
  325.     pb.dirInfo.ioFDirIndex = -1;
  326.     err = PBGetCatInfoSync((CInfoPBPtr) &pb);
  327.  
  328.     if (err == noErr) 
  329.     {
  330.         GetDateTime(&pb.dirInfo.ioDrMdDat);
  331.         err = PBSetCatInfoSync((CInfoPBPtr) &pb);
  332.     }
  333.  
  334.     return err;
  335. }
  336.  
  337. /***********************************|****************************************/
  338. //
  339. //    Move a file named fileName on the volume vRefNum from the directory sourceDirID to
  340. //    the directory destinationDirID.
  341. //
  342. OSErr MoveHFSFile(short vRefNum, long sourceDirID, const StringPtr fileName, long destinationDirID)
  343. {
  344.     OSErr err = HDelete ( vRefNum, destinationDirID, fileName );
  345. #if debug
  346.     if ( err != fnfErr )
  347.         chris << "MoveHFSFile is deleting file “" << fileName << "”; is that alright??\n";
  348. #endif
  349.  
  350. // now move the file
  351.     CMovePBRec    pb;
  352.  
  353.     memset(&pb, 0, sizeof(pb));
  354.     pb.ioNamePtr = fileName;
  355.     pb.ioVRefNum = vRefNum;
  356.     pb.ioDirID = sourceDirID;
  357.  
  358.     pb.ioNewName = nil; // use ioNewDirID
  359.     pb.ioNewDirID = destinationDirID;
  360.  
  361.     err = PBCatMoveSync(&pb);
  362.  
  363.     if (err == noErr) {
  364.         ForceFinderToUpdateFolder(vRefNum, sourceDirID);
  365.         ForceFinderToUpdateFolder(vRefNum, destinationDirID);
  366.     };
  367.  
  368.     return err;
  369. }
  370.  
  371. /***********************************|****************************************/
  372.  
  373. pascal void BLJDUc2rString (const char* cStr, CharacterSet charSet, RString *rStr, unsigned short rStrLength) {
  374. #if false
  375.     DUc2rString(cStr,charSet,rStr,rStrLength);
  376. #endif
  377.     rStr->dataLength = strlen(cStr);
  378.     if (rStrLength < rStr->dataLength)
  379.         rStr->dataLength = rStrLength;
  380.     strncpy((char*) &(rStr->body[0]),cStr,rStr->dataLength);
  381.     rStr->charSet = charSet;
  382. }
  383.  
  384. /***********************************|****************************************/
  385.  
  386. #ifndef THINK_CPLUS
  387. #ifndef    __CURSORCTL__
  388. #include "CursorCtl.h"
  389. #endif
  390.  
  391.  
  392. void Busy ( short i )
  393. {
  394.     SpinCursor ( i );
  395. }
  396. #endif
  397. /***********************************|****************************************/
  398.